package cn.joyfulmedia.controller.system.role;


import cn.joyfulmedia.common.util.Const;
import cn.joyfulmedia.common.util.page.Pagination;
import cn.joyfulmedia.entity.ExamMisResources;
import cn.joyfulmedia.entity.ExamMisRole;
import cn.joyfulmedia.mis.service.interfaces.ResourcesService;
import cn.joyfulmedia.mis.service.interfaces.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/role")
public class RoleController {
    @Resource
    ResourcesService resourcesService;
    @Resource
    private RoleService roleService;

    @RequestMapping("/list")
    public String list(ExamMisRole role, Pagination pagination, Model model) {
        List<ExamMisRole> list = roleService.listPage(role, pagination);
        model.addAttribute("list", list);
        model.addAttribute("pagination", pagination);
        return "/role/list";
    }

    @ResponseBody
    @RequestMapping("/getById/{id}")
    public Object getById(@PathVariable Long id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Const.STATUS, -1);
        try {
            ExamMisRole a = roleService.selectRoleById(id);
            map.put(Const.STATUS, 1);
            map.put("data", a);
        } catch (Exception e) {
            e.printStackTrace();
            map.put(Const.MSG, Const.DEFAULT_ERROR);
        }
        return map;
    }

    @RequiresPermissions("sys:role:add")
    @RequestMapping("/add")
    public String add(Long id, Model model) {
        model.addAttribute("id", id);
        return "/role/add";
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(ExamMisRole role) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Const.STATUS, -1);
        try {
            Integer i = 0;
            if (role.getId() != null && role.getId() != 0) {
                i = roleService.updateRoleById(role);
            } else {
                i = roleService.saveRole(role);
            }
            if (i > 0) {
                map.put(Const.STATUS, 0);
                map.put("data", "保存成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put(Const.MSG, Const.DEFAULT_ERROR);
        }
        return map;
    }

    @RequestMapping("/editPerm")
    public String editPerm(Long id, Model model) {
        try {
            ExamMisResources resource = new ExamMisResources();
            resource.setDisabled((byte) 0);
            List<ExamMisResources> list = resourcesService.list(resource);
            List<ExamMisResources> l = resourcesService.findByRoleId(id);

            for (ExamMisResources r : list) {
                for (ExamMisResources ownR : l) {
                    if (ownR.getId() != null && r.getId() != null && ownR.getId().intValue() == r.getId().intValue()) {
                        r.setChecked(Const.CHECKED);
                        break;
                    }
                }
            }
            model.addAttribute("list", list);
            model.addAttribute("id", id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/role/editPerm";
    }

    @RequestMapping("/savePerm")
    public String savePerm(Long id, Long[] rid) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Const.STATUS, -1);
        try {
            roleService.delPermBYId(id);
            roleService.bathSavePerm(rid, id);
        } catch (Exception e) {
            e.printStackTrace();
            map.put(Const.MSG, Const.DEFAULT_ERROR);
        }
        return "redirect:/role/list";
    }
}
